Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data

Namespace sk.DAL
    Public MustInherit Class ArticleProvider
        Inherits DataAccess
        Sub New()
            Me.ConnectionString = Globals.Settings.Article.ConnectionString
        End Sub
        Private Shared _instance As ArticleProvider = Nothing
        Public Shared ReadOnly Property Instance() As ArticleProvider
            Get
                If IsNothing(_instance) Then
                    _instance = CType(Activator.CreateInstance(Type.GetType(Globals.Settings.Article.ProviderType)), ArticleProvider)
                End If

                Return _instance
            End Get
        End Property

        Public MustOverride Function GetAllArticles() As Generic.List(Of Article)
        Public MustOverride Function CreateArticle(ByVal Atcle As Article) As Boolean
        Public MustOverride Function DeleteArticle(ByVal ArticleID As Integer) As Boolean
        Public MustOverride Function UpdateArticle(ByVal Atcle As Article) As Boolean
        Public MustOverride Function GetArticleByID(ByVal AtcleID As Integer) As Article
        Public MustOverride Function GetArticlesOnExpireDate(ByVal ArticleDate As Date) As Generic.List(Of Article)
        Public MustOverride Function GetArticlesOnPostDate(ByVal ArticleDate As Date) As Generic.List(Of Article)
        Public MustOverride Function GetArticlesOnCreationDate(ByVal ArticleDate As Date) As Generic.List(Of Article)
        Public MustOverride Function ChangeArticleApproval(ByVal ArticleID As Integer, ByVal status As Boolean) As Boolean
        Public MustOverride Function UpdateArticleText(ByVal ArticleId As Integer, ByVal updatedText As String) As Boolean
        Public MustOverride Function UpdateExpireDate(ByVal ArticleID As Integer, ByVal newExpireDate As Date) As Boolean
        Public MustOverride Function UpdatePostDate(ByVal ArticleID As Integer, ByVal newPostDate As Date) As Boolean
        Public MustOverride Function GetArticlesByUserID(ByVal ArticleID As String) As Generic.List(Of Article)
        Public MustOverride Function GetNextID() As Integer
        Public MustOverride Function GetCurrentArticles() As Generic.List(Of Article)

        Protected Overridable Function GetArticleFromReader(ByVal reader As IDataReader) As Article

            Dim art As New Article
            With art
                If IsDBNull(reader("ArticleID")) Then .ArticleID = 0 Else .ArticleID = CInt(reader("ArticleID"))
                If IsDBNull(reader("UserID")) Then .UserID = "" Else .UserID = reader("UserID").ToString
                If IsDBNull(reader("Title")) Then .Title = "" Else .Title = reader("Title").ToString
                If IsDBNull(reader("ArticleText")) Then .Text = "" Else .Text = reader("ArticleText").ToString
                If IsDBNull(reader("CreationDate")) Then .CreationDate = New Date(1900, 1, 1) Else .CreationDate = CDate(reader("CreationDate"))
                If IsDBNull(reader("ExpireDate")) Then .ExpirationDate = New Date(1900, 1, 1) Else .ExpirationDate = CDate(reader("ExpireDate"))
                If IsDBNull(reader("PostDate")) Then .PostDate = New Date(1900, 1, 1) Else .PostDate = CDate(reader("PostDate"))
                If IsDBNull(reader("Approved")) Then .Approved = True Else .Approved = CType(reader("Approved"), Boolean)
                If IsDBNull(reader("CategoryID")) Then .CategoryID = 0 Else .CategoryID = CInt(reader("CategoryID"))

            End With
            Return art
        End Function

        Protected Overridable Function GetArticleCollectionFromReader( _
                ByVal reader As IDataReader) _
                As List(Of Article)
            Dim ArticleList As New List(Of Article)
            Try
                While (reader.Read)
                    ArticleList.Add(GetArticleFromReader(reader))
                End While
            Catch ex As Exception
                Throw New Exception("from GetArticleCollectionFromReader", ex)
            End Try
            Return ArticleList
        End Function

    End Class
End Namespace


